package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.impl.CategoryServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/category")
@Api(tags = "分类管理")
public class CategoryController {
    @Autowired
    private CategoryServiceImpl categoryService;

    /**
     * 分类分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分类分页查询")
    public Result<PageResult> getAllCategory(CategoryPageQueryDTO categoryPageQueryDTO){
        return Result.success(categoryService.getAllCategory(categoryPageQueryDTO));
    }

    /**
     * 添加分类
     * @param categoryDTO
     * @return
     */
    @PostMapping
    @ApiOperation("添加分类")
    public Result<Object> addCategory(@RequestBody CategoryDTO categoryDTO){
        categoryService.addCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 修改分类
     * @param categoryDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改分类")
    public Result<Object> updateCategory(@RequestBody CategoryDTO categoryDTO){
        categoryService.updateCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 修改状态
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("修改状态")
    public Result<Object> updateStatus(@RequestParam Long id, @PathVariable Integer status){
        categoryService.updateStatus(id,status);
        return Result.success();
    }

    /**
     * 按照id 删除 分类
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("按照id 删除 分类")
    public Result<Object> deleteCategory(@RequestParam Long id){
        categoryService.deleteCategory(id);
        return Result.success();
    }

    /**
     * 按照 类型查询分类
     * @param type
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("按照 类型查询分类")
    public Result<List<Category>> getCategoryByType(@RequestParam Integer type){
        return Result.success(categoryService.getCategoryByType(type));
    }

}
